# PowerShell script to convert ApiDoc format to Swagger format

function Convert-ApiDocToSwagger {
    param(
        [string]$FilePath,
        [string]$TagName
    )
    
    $content = Get-Content $FilePath -Raw -Encoding UTF8
    
    # Replace @api patterns with @swagger format
    $content = $content -replace '(?s)/\*\*\s*\*\s*@api\s+\{(get|post|put|delete)\}\s+([^\s]+)\s+([^\n]+)\s*\*\s*@apiName\s+[^\n]+\s*\*\s*@apiGroup\s+[^\n]+\s*\*\s*@apiHeader\s+\{String\}\s+token\s+token([^*]*)\*/', {
        param($match)
        $method = $match.Groups[1].Value.ToLower()
        $path = $match.Groups[2].Value
        $summary = $match.Groups[3].Value
        $additionalContent = $match.Groups[4].Value
        
        $swaggerDoc = "/**`n * @swagger`n * $path`:`n *   $method`:`n *     summary: $summary`n *     tags: [$TagName]`n *     security:`n *       - bearerAuth: []`n"
        
        # Add parameters if it's a GET request with @apiParam
        if ($method -eq 'get' -and $additionalContent -match '@apiParam') {
            $swaggerDoc += " *     parameters:`n"
            $paramMatches = [regex]::Matches($additionalContent, '@apiParam\s+\{([^}]+)\}\s+([^\s]+)\s+([^\n]+)')
            foreach ($paramMatch in $paramMatches) {
                $paramType = $paramMatch.Groups[1].Value.ToLower()
                $paramName = $paramMatch.Groups[2].Value
                $paramDesc = $paramMatch.Groups[3].Value
                $swaggerDoc += " *       - in: query`n *         name: $paramName`n *         required: true`n *         schema:`n *           type: $paramType`n *         description: $paramDesc`n"
            }
        }
        
        # Add requestBody if it's a POST/PUT request with @apiParam
        if (($method -eq 'post' -or $method -eq 'put') -and $additionalContent -match '@apiParam') {
            $swaggerDoc += " *     requestBody:`n *       required: true`n *       content:`n *         application/json:`n *           schema:`n *             type: object`n *             properties:`n"
            $paramMatches = [regex]::Matches($additionalContent, '@apiParam\s+\{([^}]+)\}\s+([^\s]+)\s+([^\n]+)')
            $requiredParams = @()
            foreach ($paramMatch in $paramMatches) {
                $paramType = $paramMatch.Groups[1].Value.ToLower()
                $paramName = $paramMatch.Groups[2].Value
                $paramDesc = $paramMatch.Groups[3].Value
                $swaggerDoc += " *               $paramName`:`n *                 type: $paramType`n *                 description: $paramDesc`n"
                $requiredParams += $paramName
            }
            if ($requiredParams.Count -gt 0) {
                $swaggerDoc += " *             required:`n"
                foreach ($param in $requiredParams) {
                    $swaggerDoc += " *               - $param`n"
                }
            }
        }
        
        $swaggerDoc += " *     responses:`n *       200:`n *         description: 操作成功`n *         content:`n *           application/json:`n *             schema:`n *               type: object`n *               properties:`n *                 code:`n *                   type: string`n *                   example: '200'`n *                 message:`n *                   type: string`n *                   example: '$summary'`n */"
        
        return $swaggerDoc
    }
    
    Set-Content $FilePath $content -Encoding UTF8
}

# Convert admin files
$adminFiles = @(
    'address.js', 'admin-menu.js', 'admin-role.js', 'banner.js', 'community.js',
    'dailyReport.js', 'data.js', 'file.js', 'notice-category.js', 'notice.js',
    'post-category.js', 'post-collection.js', 'pro.js', 'search.js', 'statistic.js', 'todo.js'
)

foreach ($file in $adminFiles) {
    $filePath = "c:\Users\jameswatt\Desktop\server-course\hi-server-new\admin\$file"
    if (Test-Path $filePath) {
        Write-Host "Converting admin/$file to Swagger format..."
        Convert-ApiDocToSwagger -FilePath $filePath -TagName "管理端"
    }
}

# Convert api files
$apiFiles = @('file.js', 'other.js')

foreach ($file in $apiFiles) {
    $filePath = "c:\Users\jameswatt\Desktop\server-course\hi-server-new\api\$file"
    if (Test-Path $filePath) {
        Write-Host "Converting api/$file to Swagger format..."
        Convert-ApiDocToSwagger -FilePath $filePath -TagName "用户端"
    }
}

Write-Host "Conversion completed!"